package com.dxdemo.dx2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.util.Log;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;

import com.dingxiang.mobile.captcha.DXCaptchaEvent;
import com.dingxiang.mobile.captcha.DXCaptchaListener;
import com.dingxiang.mobile.captcha.DXCaptchaView;
import com.dxdemo.R;

import java.lang.ref.WeakReference;
import java.util.Map;

public class Dxdialog {
    private static final String TAG = "Dxdialog";
    private static Dialog mDXDialog;
    private static WeakReference<Activity> mActivity;

    /**
     * 打开dx
     */
    public static void show(final Activity activity, final int themeResId) {
        if (activity == null) return;
        mActivity = new WeakReference<Activity>(activity);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!activity.isFinishing()) {
                    mDXDialog = new Dialog(activity, themeResId);
                    mDXDialog.setContentView(R.layout.dxdialog);

                    DXCaptchaView dxCaptcha = (DXCaptchaView) mDXDialog.findViewById(R.id.dxCaptcha);
                    dxCaptcha.init("b55965652903be3cac2c8683ecd9091f");

                    dxCaptcha.startToLoad(new DXCaptchaListener() {
                        @Override
                        public void handleEvent(WebView webView, DXCaptchaEvent dxCaptchaEvent, Map<String, String> map) {
                            switch (dxCaptchaEvent) {
                                case DXCAPTCHA_SUCCESS: // 验证成功的回调
                                    String token = map.get("token"); // 成功时会传递token参数
                                    Log.i(TAG, "Verify Success. token: " + token);
                                    hide(activity);
                                    break;
                                case DXCAPTCHA_FAIL:      // 验证失败回调
                                    Log.i(TAG, "Verify Failed.");
                                    break;
                                default:
                                    Log.i(TAG, "Unknown");
                                    break;
                            }
                        }
                    });


                    Window window = mDXDialog.getWindow();
                    mDXDialog.setCancelable(true);

                    if (!mDXDialog.isShowing()) {
                        mDXDialog.show();
                    }
                }
            }
        });
    }

    /**
     * 打开dx
     */
    public static void show(final Activity activity) {
        int resourceId = R.style.SplashScreen_SplashTheme;
        show(activity, resourceId);
    }

    /**
     * 关闭dx
     */
    public static void hide(Activity activity) {
        if (activity == null) {
            if (mActivity == null) {
                return;
            }
            activity = mActivity.get();
        }

        if (activity == null) return;

        final Activity _activity = activity;

        _activity.runOnUiThread(new Runnable() {
            @SuppressLint("ObsoleteSdkInt")
            @Override
            public void run() {
                if (mDXDialog != null && mDXDialog.isShowing()) {
                    boolean isDestroyed = false;

                    DXCaptchaView dxCaptcha = (DXCaptchaView) mDXDialog.findViewById(R.id.dxCaptcha);

                    ((ViewGroup) dxCaptcha.getParent()).removeView(dxCaptcha);

                    dxCaptcha.destroy();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        isDestroyed = _activity.isDestroyed();
                    }

                    if (!_activity.isFinishing() && !isDestroyed) {
                        mDXDialog.dismiss();
                    }
                    mDXDialog = null;
                }
            }
        });
    }

}
